package com.tracker

import java.util.List;
import com.commonProject.common.exception.ValidateException
import com.commonProject.common.classes.TurDTO
import com.commonProject.common.intefaces.ITur


class TurService {

    static transactional = true	
	ITur turServiceProxy 

	
	def getlist(params){
		List<TurDTO> el=null		
		
		println("TurService Test")		
		Long tur_id=Long.valueOf(params.get("id"))
		el=turServiceProxy.getlist(tur_id)
			
		return el.collect{[
			text : it.title,
			id: it.id	
		]}		
	}
 
	
	def getelement(params){
		println(params)
		Long tur_id=Long.valueOf(params.get("id"))
		TurDTO el=null;
		println("tur_id ="+tur_id)
		try{
			el=turServiceProxy.getelement(tur_id)
		} catch(Exception e) {
			//println "-->EXCEPTION: ${ex}"
			println "No server connection"
			return ['success':'false']
		}			
		return [
			'success' : 'true',
			id : el.id,
			title : el.title,
			description : el.description,
			dateCreated : el.dateCreated
			]		
	}

	def deleteElement(params){		
		Long tur_id=Long.valueOf(params.get("id"))
		TurDTO el=null;		
		println("Delete Tur Client")
		try{
			el=turServiceProxy.deleteElement(tur_id)		
		} catch(Exception e) {
			//println "-->EXCEPTION: ${ex}"
			println "No server connection"
			return [success:false]
		}
		return [success:true]
	}
	
	
	def addElement(params) {
		println("ADD TUR")
		println(params)	
				
		Long tur_id=Long.valueOf(params.get('id'))
		String tur_title=params.get('title')
		String tur_description=params.get('description')
	
		try{
			turServiceProxy.addElement(tur_id,tur_title,tur_description)
		}catch(ValidateException err){
		println(err.getError())

			
			return [
				'success' :'false',
				errors :err.getError()	
				]
		}
		return ['success':'true']
		
	}
	
	
	def editElement(params){
		println("TUR EDIT")
		println(params)
		
		Long tur_id=Long.valueOf(params.get('id')) 
		String tur_title=params.get('editTurTitle')
		String tur_description=params.get('editTurDescription')
		String tur_dateCreated=params.get('editTurDateCreated')
		println("tur_dateCreated"+tur_dateCreated)
		
		try{
			def el=turServiceProxy.editElement(tur_id,tur_title,tur_description,tur_dateCreated)
		}catch(ValidateException err){
		println(err.getError())
			return [
				'success' :'false',
				errors :err.getError()	
				]
		}
		return ['success':'true']
	}
	
		
	
	
		/*def company=Company.get(params.get('id'))
		if (company) {
			def newTur=new Tur()
			newTur.title=params.get('title')
			newTur.description=params.get('description')			
			newTur.dateCreated = new Date()		
			company.addToTur(newTur)
			
			if (newTur.save()) {
				return ['success':'true']
			}else {
				def err = newTur.errors.allErrors.collect{[
				errorMessage:messageSource.getMessage(it, null)
			]}
			println(err)
			return [
				success :'false',
				errors :err				
				]
			}
		}else {
			return ['success':'false']
		}*/
	
	
/*		
	def editElement(params){
		println("TUR EDIT")
		def edit = Tur.get(params.get('id'))
		edit.title = params.get('editTurTitle')
		edit.description=params.get('editTurDescription')
		
		if (params.get('editTurDateCreated')){
			edit.dateCreated = new Date(params.get('editTurDateCreated'))
		}else{
			edit.dateCreated = null
		}
		if(edit.save()){
			return['success':'true']
		}else{		
			println("******")
			/*edit.errors.allErrors.each{
				println "${it}"
				println messageSource.getMessage(it, null)
			}*/
			/*def err = edit.errors.allErrors.collect{[
				errorMessage:messageSource.getMessage(it, null)
			]}
			println(err)
			return [
				'success':'false',
				errors:err				
				]
		}		
	}
	
	
	
}*/	
}